package com.lehuo.jyn;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.lehuo.util.ConnUtil;
import com.lehuo.util.RepairUtil;
import com.lehuo.util.StringUtil;
import com.lehuo.util.UserUtil;

public class LehuoServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("doGet");
		doPost(req, resp);
	}

	@SuppressWarnings("unused")
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf-8");
		resp.setCharacterEncoding("utf-8");
		req.setCharacterEncoding("utf-8");
		if (req == null) {
			return;
		}
		String action = req.getParameter("action");
		System.out.println("doPost action:" + action);
		switch (action) {
		case "login":
			login(req, resp);
			break;
		case "register":
			register(req, resp);
			break;
		case "getUserById":
			getUserById(req, resp);
			break;
		case "updataUser":
			updataUser(req, resp);
			break;
			
		case "addRepair":
			addRepair(req, resp);
			break;
		case "getRepair":
			getRepair(req, resp);
			break;
		case "updataRepair":
			updataRepair(req, resp);
			break;
		case "deleteRepair":
			deleteRepair(req, resp);
			break;
		default:
			break;
		}
	}

	public void login(HttpServletRequest req, HttpServletResponse resp)
			throws UnsupportedEncodingException {

		String username = req.getParameter("username");
		String password = req.getParameter("password");
		username = StringUtil.toutf(username);
		password = StringUtil.toutf(password);
		Connection connection = null;
		try {
			connection = ConnUtil.getConnection();
			connection.setAutoCommit(false);
			String callback = UserUtil.getUserMsg(username, password,
					connection);
			System.out.println("getUser connection:" + callback);
			resp.getWriter().print(callback);
			resp.getWriter().close();
		} catch (IOException | SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				connection.setAutoCommit(true);
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public void register(HttpServletRequest req, HttpServletResponse resp) {
		String nickname = req.getParameter("nickname");
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String thumbnail = req.getParameter("thumbnail");
		nickname = StringUtil.toutf(nickname);
		username = StringUtil.toutf(username);
		password = StringUtil.toutf(password);
		thumbnail = StringUtil.toutf(thumbnail);
		Connection connection = null;
		try {
			connection = ConnUtil.getConnection();
			connection.setAutoCommit(false);
			String result = UserUtil.register(nickname, username, password,
					thumbnail, connection);
			resp.getWriter().print(result);
			resp.getWriter().close();
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		} finally {
			try {
				connection.setAutoCommit(true);
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public void getUserById(HttpServletRequest req, HttpServletResponse resp) {
		String id = req.getParameter("id");
		id = StringUtil.toutf(id);
		Connection connection = null;
		try {
			connection = ConnUtil.getConnection();
			connection.setAutoCommit(false);
			String result = UserUtil.getUserById(id, connection);
			resp.getWriter().print(result);
			resp.getWriter().close();
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		} finally {
			try {
				connection.setAutoCommit(true);
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void updataUser(HttpServletRequest req, HttpServletResponse resp) {
		String nickname = req.getParameter("nickname");
		String password = req.getParameter("password");
		String thumbnail = req.getParameter("thumbnail");
		String tel = req.getParameter("tel");
		String id = req.getParameter("id");

		nickname = StringUtil.toutf(nickname);
		password = StringUtil.toutf(password);
		thumbnail = StringUtil.toutf(thumbnail);
		tel = StringUtil.toutf(tel);
		id = StringUtil.toutf(id);
		Connection connection = null;
		
		try {
			connection = ConnUtil.getConnection();
			connection.setAutoCommit(false);
			String callback = UserUtil.updataUser(id, nickname, password, thumbnail, tel, connection);
			System.out.println("updataUser connection:" + callback);
			resp.getWriter().print(callback);
			resp.getWriter().close();
		} catch (IOException | SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				connection.setAutoCommit(true);
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void addRepair(HttpServletRequest req, HttpServletResponse resp) {
		String user_id = req.getParameter("user_id");
		String type = req.getParameter("type");
		String project = req.getParameter("project");
		String describe = req.getParameter("describe");
		String username = req.getParameter("username");
		String tel = req.getParameter("tel");
		String reser_time = req.getParameter("reser_time");
		String address = req.getParameter("address");
		String reser_type = req.getParameter("reser_type");
		String time = req.getParameter("time");
		
		user_id = StringUtil.toutf(user_id);
		type = StringUtil.toutf(type);
		project = StringUtil.toutf(project);
		describe = StringUtil.toutf(describe);
		username = StringUtil.toutf(username);
		tel = StringUtil.toutf(tel);
		reser_time = StringUtil.toutf(reser_time);
		address = StringUtil.toutf(address);
		reser_type = StringUtil.toutf(reser_type);
		time = StringUtil.toutf(time);
		
		Connection connection = null;
		try {
			connection = ConnUtil.getConnection();
			connection.setAutoCommit(false);
			String result = RepairUtil.addRepair(user_id, type, project,
					username, describe, tel, reser_time, address, reser_type,
					time, connection);
			System.out.println("addRepair result:" + result);
			resp.getWriter().print(result);
			resp.getWriter().close();
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		} finally {
			try {
				connection.setAutoCommit(true);
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public void getRepair(HttpServletRequest req, HttpServletResponse resp) {
		String user_id = req.getParameter("user_id");
		Connection connection = null;
		try {
			connection = ConnUtil.getConnection();
			connection.setAutoCommit(false);
			String result = RepairUtil.getRepair(user_id, connection);
			System.out.println("getRepair result:" + result);
			resp.getWriter().print(result);
			resp.getWriter().close();
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		} finally {
			try {
				connection.setAutoCommit(true);
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public void updataRepair(HttpServletRequest req, HttpServletResponse resp) {
		String id = req.getParameter("id");
		String type = req.getParameter("type");
		String project = req.getParameter("project");
		String describe = req.getParameter("describe");
		String username = req.getParameter("username");
		String tel = req.getParameter("tel");
		String reser_time = req.getParameter("reser_time");
		String address = req.getParameter("address");
		String reser_type = req.getParameter("reser_type");
		String time = req.getParameter("time");
		
		id = StringUtil.toutf(id);
		type = StringUtil.toutf(type);
		project = StringUtil.toutf(project);
		describe = StringUtil.toutf(describe);
		username = StringUtil.toutf(username);
		tel = StringUtil.toutf(tel);
		reser_time = StringUtil.toutf(reser_time);
		address = StringUtil.toutf(address);
		reser_type = StringUtil.toutf(reser_type);
		time = StringUtil.toutf(time);
		
		Connection connection = null;
		try {
			connection = ConnUtil.getConnection();
			connection.setAutoCommit(false);
			String result = RepairUtil.updataRepair(id, type, project,
					username, describe, tel, reser_time, address, reser_type,
					time, connection);
			System.out.println("addRepair result:" + result);
			resp.getWriter().print(result);
			resp.getWriter().close();
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		} finally {
			try {
				connection.setAutoCommit(true);
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public void deleteRepair(HttpServletRequest req, HttpServletResponse resp) {
		String id = req.getParameter("id");
		Connection connection = null;
		try {
			connection = ConnUtil.getConnection();
			connection.setAutoCommit(false);
			String result = RepairUtil.deleteRepair(id, connection);
			System.out.println("addRepair result:" + result);
			resp.getWriter().print(result);
			resp.getWriter().close();
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		} finally {
			try {
				connection.setAutoCommit(true);
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
} 
